﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using TLL.UTL;
using TLL.Finance.BankSys;

namespace TasksLog
{
    public partial class PBBalance : Form
    {
        private bool MouseClick = false;
        private Loader Loader = null;
        private Point MouseClickPosition;
        private bool access = false;

        public bool Access { get { return this.access; } }

        List<BankMerchant> merchants = new List<BankMerchant>();
        public PBBalance()
        {
            InitializeComponent();
            InitializeCards();
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        private void InitializeCards()
        {
            if (Loader == null)
                Loader = new Loader(this, LoaderType.Diagram);

            Loader.Start();

            using (var repository = new MerchantRepository(Parametrs.DebugURL, Parametrs.CodingKey))
                if (repository.Access)
                    this.merchants = repository.Get();

            var privateAPI = new PrivateAPI("114", this.merchants);
            var res = privateAPI.GeBalances();

            if (res != null)
            {
                foreach (var card in res)
                {
                    LBCardName.Items.Add(card.Name);
                    LBCardNumber.Items.Add(card.Number);
                    LBCardCurrency.Items.Add(card.Currency.ToString());
                    LBCardBalance.Items.Add(card.Balance.ToString());
                }
                this.access = true;
            }
            Loader.Stop();
            this.WindowState = FormWindowState.Normal;
        }

        private void LB_SelectedIndexChanged(object sender, EventArgs e)
        {
            var LB = sender as ListBox;

            LBCardName.SelectedIndex = LB.SelectedIndex;
            LBCardNumber.SelectedIndex = LB.SelectedIndex;
            LBCardCurrency.SelectedIndex = LB.SelectedIndex;
            LBCardBalance.SelectedIndex = LB.SelectedIndex;
        }

        private void BtClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtInDock_Click_1(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

            this.MouseClick = true;
            this.MouseClickPosition = new Point(e.Location.X, e.Location.Y);
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            this.MouseClick = false;
            this.MouseClickPosition = Point.Empty;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.MouseClick)
            {
                this.Top = MousePosition.Y - this.MouseClickPosition.Y;
                this.Left = MousePosition.X - this.MouseClickPosition.X;
            }
        }

        private void LB_DoubleClick(object sender, EventArgs e)
        {
            var LB = sender as ListBox;

            var detailForm = new DetailCard(LBCardNumber.Items[LB.SelectedIndex].ToString(), this.merchants);
            detailForm.Show();
        }
    }
}
